<?php


class FileLink
{
    public $sign;  // 标识
    public $fix;  // 后缀
    const FILE_PATH = '/web/muggle/static/public/image-base';

    public function __construct()
    {
        if (empty($_FILES)) {
            echo json_encode(array(
                'code' => -700,
                'mess' => urlencode('您的请求未能成功'),
                'datetime' => time(),
                'data' => array(
                    'code' => -1,
                    'mess' => urlencode('未能获取上传文件')
                )
            ));
            return false;
        }
        if (empty($_POST['sign']) || empty($_POST['fix'])) {
            echo json_encode(array(
                'code' => -555,
                'mess' => urlencode('您的请求未能成功'),
                'datetime' => time(),
                'data' => array(
                    'code' => -1,
                    'mess' => urlencode('附带参数缺失')
                )
            ));
            return false;
        }
        $this->sign = $_POST['sign'];
        $this->fix = $_POST['fix'];
    }

    public function save()
    {
        $fname = $_FILES[$this->sign]['name'];
        $tmp_file = $_FILES[$this->sign]["tmp_name"];
        $localpath = self::FILE_PATH . '/art-image/';  // 文件最终路径
        $fname = 'uid_'. time(). '.'. $this->fix;
        move_uploaded_file($tmp_file, $localpath. $fname);
        echo json_encode(array(
            'code' => 0,
            'mess' => urlencode('请求成功'),
            'datetime' => time(),
            'data' => array(
                'code' => 0,
                'mess' => urlencode('文件保存成功~'),
                'src' => '/public/image-base/art-image/'. $fname
            )
        ));
        return true;
    }

    public function app()
    {
        echo json_encode(array(
            'code' => 123
        ));
    }
}